package com.workspace.controller;

import com.workspace.daomain.Customers;
import com.workspace.daomain.Photo;
import com.workspace.repository.CustomerRepository;
import com.workspace.service.CustomerService;
import com.workspace.service.PhotoSevice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

@Controller
public class UserController {
    @Autowired
    private CustomerRepository customerRepository;
    @Autowired
    private CustomerService customerService;
    @Autowired
    private PhotoSevice photoSevice;
    //登录跳转
    @GetMapping("/toLogin")
    public String toLogin(HttpSession session) {
        if (session.getAttribute("user")!=null){
            session.removeAttribute("user");
            return "/login/index";
        }else
        return "/login/index";
    }

    //登录状态反馈
    @RequestMapping("/loginstatus")
    public String loginstatus(@RequestParam("username") String username,
                              @RequestParam("password") String password,
                              Model model, HttpSession session) {
        Customers customers = customerService.findCustomerByUsername(username);
        if (!password.trim().equals(customers.getPassword())) {
            model.addAttribute("reminder", "请确认是否输入信息，并且检查用户名和密码");
            return "/login/index";
        } else
            session.setAttribute("user", customers);
        return "index";


    }

    //退出登录
    @GetMapping("/loginout")
    public String loginOut(HttpSession session) {
        session.removeAttribute("user");
        return "/login/index";
    }

    //用户注册
    @GetMapping("/toRegister")
    public String toRegister() {
        return "/register/register";
    }

    //注册状态反馈
    @RequestMapping("/registerstatus")
    public String registerstatus(@RequestParam("username") String username,
                                 @RequestParam("password1") String password1,
                                 @RequestParam("password2") String password2,
                                 Model model,
                                 HttpSession session) {
        Customers customers1= customerService.findCustomerByUsername(username);
        if (customers1!=null){
            model.addAttribute("reminder", "对不起，用户名已经存在！！！");
            return "/register/register";
        }
       else if (username.length() <= 0 || password1.length() <= 0 || password2.length() <= 0) {
            model.addAttribute("reminder", "对不起，信息不完整！！！");
            return "/register/register";
        } else if (!password1.trim().equals(password2.trim())) {
            model.addAttribute("reminder", "对不起，两次密码不一致");
            return "/register/register";
        }
        //加载主类
        Customers customers = new Customers();
        //系统加载注册时间
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String d = format.format(date);
        //系统设置权限
        String role = "common";
        //注册用户名
        customers.setUsername(username);
        //注册密码
        customers.setPassword(password1);
        //注册日期
        customers.setRdate(d);
        //注册权限
        customers.setRole(role);
        //提交
        customerRepository.save(customers);
        session.setAttribute("user", customers);
        return "index";
    }
@GetMapping("/")
public void index(HttpServletResponse response) throws IOException {
        response.sendRedirect("/index");
}
    //主页
    @GetMapping("/index")
    public String toIndex(Model model) {
        List<Photo> photoList=photoSevice.findPhotos();
        model.addAttribute("photoList",photoList);
        return "index";
    }
    //分类跳转
    @RequestMapping("/photoclass")
    public String photoClass(@RequestParam("cid") Integer cid,Model model){
        List<Photo> photoList=photoSevice.findAllPhotosByCid(cid);
model.addAttribute("photoList",photoList);
return "index";

    }
    //后台登录
    @GetMapping("/adminLogin")
    public String adminLogin(HttpSession session){
        if (session.getAttribute("admin")!=null)
            return "/admin/admin";
        else
        return "admin/adminlogin";
    }

}
